<?php 
/**
  * 角色控制器
  * date 2013-1-27 
  */
class RoleAction extends CommonAction {

	public function _initialize()
	{
    $this->currentModel = D(MODULE_NAME);    
		parent::_initialize();	
	}

  public function index()
  {
    $map=$this->_search();
    $count=$this->currentModel->where($map)->count();
    $page=$this->_page($count,C('PAGE_SUM'));
    $datas=$this->currentModel->where($map)->limit($page->firstRow.','.$page->listRows)->order($this->pkId.' DESC')->select();
    $this->assign('page',$page->show());
    $this->assign('datas',$datas);    
    $this->display();
  }

  public function add()
  {
    if($this->isPost())
    {
      $this->_save();
    }else
        {
          $this->display();
        }
  }

  public function edit()
  {
    if($this->isPost())
    {
      $this->_save();
    }else
        {
          $id=$this->_get($this->pkId);
          $data=$this->currentModel->find($id);
          if(empty($data))$this->error('非法操作',U(MODULE_NAME.'/index'));
          $this->assign('data',$data);
          $this->display('add');
        }
  }


  /**
  * 授权
  * @access public
  */
  public function accredit()
  {
    if($this->isPost())
    {
      $access=M('Access');
      $roleId=$this->_get($this->pkId);
      $nodeStr=$_POST['node'];
      $access->where(array('role_id'=>$roleId))->delete();
      if($nodeStr!='')
      {
        foreach(explode('#',$nodeStr) as $node)
        {
          $arr=explode('-',$node);
          $data['role_id']=$roleId;
          $data['node_id']=$arr[0];
          $data['parent_id']=$arr[1];
          $data['level']=$arr[2];
          $access->data($data)->add();
        }
      }
      echo 1;
    }else
        {
          $roleId=$this->_get($this->pkId);
          $data=$this->currentModel->field('id')->find($roleId);
          if(empty($data))$this->error('非法操作',U(MODULE_NAME.'/index'));          
          if($this->isAjax())
          {
            $html='[';
            $access=M('Access');
            $node=M('Node');
            $map['role_id']=$roleId;
            $data=$access->field('node_id')->where($map)->select();
            $nodes=$node->select();
            if(empty($data))
            {
              foreach($nodes as $node)
              { 
                $opened=$node['level']==1?',open:true':'';
                $html.="{id:".$node['id'].", pId:".$node['parent_id'].", name:'".$node['title']."'".$opened."},";
              }    
            }else
                {
                  foreach($data as $d)
                  {
                    $current[]=$d['node_id'];
                  }
                  foreach($nodes as $node)
                  {
                    $checked=in_array($node['id'],$current)?',checked:true':'';
                    $opened=$node['level']==1?',open:true':'';
                    $html.="{id:".$node['id'].", pId:".$node['parent_id'].", name:'".$node['title']."'".$checked.$opened."},";
                  }
                }
            $html.=']'; 
            echo $html;
          }else
              {
                $this->assign('successUrl',U(MODULE_NAME.'/index'));
                $this->assign('roleId',$roleId);
                $this->display();
              }      
        } 
  }


  /**
  * 删除数据
  * @access public
  */
  public function del()
  {
    $id=$this->_request($this->pkId);
    if(is_array($id))$id=implode(',',$id);
    $this->_haveAdminByRole($id);
    if($this->currentModel->delete($id)>0)
    {
      $this->_delAccessByRole($id);
      $this->success('成功',U(MODULE_NAME.'/index'));
    }else
      {
        $this->error('失败',U(MODULE_NAME.'/index'));
      }
  }

  /**
   * 删除角色与权限的映射数据
   * @access private
   * @param string $roleId
   */
  private function _delAccessByRole($roleId)
  {
    $where['role_id']=array('in',$roleId);
    M('Access')->where($where)->delete();
  }

 /**
  * 判断当前角色下是否存在账号
  * @access private
  * @param $id 角色ID
  */
  private function _haveAdminByRole($id)
  {
    $where['role_id']=array('in',$id);
    $count=M('Administrator')->where($where)->count();
    if($count>0)$this->error('当前角色下存在账号，不能删除',U(MODULE_NAME.'/index'));
  }

  /**
  * 改变信息状态
  * @access public
  */
  public function change()
  {
    $id=$this->_post($this->pkId);
    if(empty($id))$this->error('请选择操作信息',U(MODULE_NAME.'/index'));
    $funcName=$this->_get('type');
    if(method_exists($this->currentModel,$funcName))
    {
      foreach($id as $v)
      {
        $this->currentModel->$funcName(array('id'=>$v));
      }
    }else{$this->error('非法操作',U(MODULE_NAME.'/index'));}
    $this->success('成功',U(MODULE_NAME.'/index'));
  }  

  private function _save()
  {
      if($this->currentModel->create()===FALSE)
      {
        $this->error($this->currentModel->getError());
      }
      $id=$this->_post($this->pkId);
      $funcName=$id>0?'save':'add';
      if($this->currentModel->$funcName()!==false)
      {
        $this->success('成功',U(MODULE_NAME.'/index'));
      }else
          {
            $this->error('失败');
          }      
  }  

}	
 ?>